package myproject.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String home(){

        return "home";
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){

        return "hello";
    }

    @RequestMapping(value = "/hello1/apply/applyInfo.do",method = RequestMethod.POST)
    @ResponseBody
    public String method1(@RequestBody String string){

        System.out.println(string);
        return "home";
    }

    @RequestMapping(value = "/aais/apply/applyInfo.",method = RequestMethod.POST)
    @ResponseBody
    public String method4(@RequestBody String string){

        System.out.println(string);
        return "{\"key\":\"home\"}";
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.POST)
    @ResponseBody
    public String method2(HttpServletRequest request) throws IOException {



        InputStream inputStream = request.getInputStream();
        int i = -1;
        List<Byte> list = new ArrayList<>();
        while ((i=inputStream.read())!=-1){
            list.add((byte) i);
        }
        Object[] objects = list.toArray();
        System.out.println(objects.length);
        byte[] bytes = new byte[objects.length];
        for (int j=0;j<objects.length;j++){
            bytes[j] = (byte) objects[j];
        }


        String string = new String(bytes);
        System.out.println(string);
        return "home";
    }

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    @ResponseBody
    public String method3(@RequestBody String string){

        System.out.println(string);
        return "home";
    }


   /* public static void main(String[] args) {
        int i = 100;
        byte b = (byte)i;
    }*/
}
